﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FTPLib;

namespace MessMain
{
    public partial class DescarcaImaginiMedicaleForm : Form
    {
        private string file;
        private string ip;
        private int port;
        private string user;
        private string pass;

        public bool success;

        public DescarcaImaginiMedicaleForm(string file, string ip, int port, string user, string pass)
        {
            InitializeComponent();

            this.file = file;
            this.ip = ip;
            this.port = port;
            this.user = user;
            this.pass = pass;
        }

        private void DescarcaImaginiMedicaleForm_Shown(object sender, EventArgs e)
        {
            int percent;
            FTP ftpClient;

            this.Refresh();
            progressBar.Minimum = 0;
            progressBar.Maximum = 100;

            try
            {
                ftpClient = new FTP();

                ftpClient.port = port;
                ftpClient.server = ip;
                ftpClient.user = user;
                ftpClient.pass = pass;

                ftpClient.OpenDownload(file);
                
                while (ftpClient.DoDownload() > 0)
                {
                    percent = (int)(((ftpClient.BytesTotal) * 100) / ftpClient.FileSize);

                    progressBar.Value = percent;
                }

                // finish
                success = true;
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error downloading file " + file + " .\r\n" +
                                "Reason: " + ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                success = false;
                this.Hide();
            }
        }
    }
}
